const router = require('koa-router')()
const util = require('../utils/util')
const { section, post, isCollect, selectPost, uPost, addPost, noneRecycle, } = require('../services/plate');

router.post('/section/:sid', async (ctx) => {
  try {
    const { sid } = ctx.request.body
    console.log(sid);
    const res = await section(sid)
    console.log(res);
    ctx.body = util.success(res)
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
router.post('/section/:sid/:pid', async (ctx) => {
  try {
    const { uid, sid, pid } = ctx.request.body
    console.log(sid, pid);
    const res = await post(sid, pid)
    const isCollects = await isCollect(uid, pid)
    console.log(res);
    ctx.body = util.success({ res, isCollects })
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
router.post('/search', async (ctx) => {
  try {
    const { text } = ctx.request.body;
    console.log(text);
    const res = await selectPost(text)
    // const nodesData = await nodes()
    // console.log(newLink, newNode, 111111111111111);

    ctx.body = util.success(res)
    // ctx.body = util.success({ nodesData, linksData })
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
router.post('/post', async (ctx) => {
  try {
    const { uid } = ctx.request.body;
    // console.log(intro);
    const res = await uPost(uid)
    // const nodesData = await nodes()
    // console.log(newLink, newNode, 111111111111111);

    ctx.body = util.success(res)
    // ctx.body = util.success({ nodesData, linksData })
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
router.post('/addPost', async (ctx) => {
  try {
    const { uid, pid } = ctx.request.body;
    const res = await addPost(uid, pid)
    ctx.body = util.success(res)
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
router.post('/noneRecycle', async (ctx) => {
  try {
    const { uid, pid } = ctx.request.body;
    const res = await noneRecycle(uid, pid)
    ctx.body = util.success('', '', res)
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

module.exports = router